<template>
  <div class="addcolor">
    <div class="bg_img">
      <headerNav />
      <searchBar :backgroundImage="backgroundImage" />
    </div>

    <div class="mindWidth flex space-between" style="margin-top:20px">
      <information
        ref="info"
        @updateList="getPolicyInfo"
        :noticeList="noticeData"
        :scramList="scramData"
        :newsList="newsData"
      />
      <government ref="gover" />
    </div>
    <bannerSwipt />
    <achievement :achieveList="achieveData" :demandList="demandData" />
    <serviceProvider />
    <navigation />
    <footerNav />

    <!-- <el-dialog
      title
      :visible.sync="festivalVisible"
      width="30%"
      :before-close="handleClose"
      class="dialog"
    >
      <div class="festival">
        <div class="photo">
          <img mode="widthFix" @click="toVip" :src="imgBaseUrl + 'static/huodong1.png'" />
        </div>
        <div class="see-btn" @click="toFestivalDetail">点击查看</div>
      </div>
    </el-dialog> -->
  </div>
</template>

<script>
import information from "../../components/home_modules/information.vue"; // 政府政策
import government from "../../components/home_modules/government.vue"; // 政府政策
import bannerSwipt from "../../components/home_modules/banner_swipt.vue"; // 轮播图
import serviceProvider from "../../components/home_modules/serviceProvider.vue"; // 轮播图
import searchBar from "../../components/home_modules/search.vue"; // 搜索框
import {
  queryPolicyList,
  queryPolicyListSsr,
  interpretList,
  interpretListSsr,
  newsList,
  newsListSsr,
  getCacheAreaInfo,
  getPageMetaInfo,
  getSubInfo,
  achievementsList,
  achievementsListSsr,
  demandList,
  demandListSsr,
  imgBaseUrl
} from "../../api/api.js";
import {
  getHomeBackgroundConfigInfo,
  getHomeBackgroundConfigInfoSsr
} from "../../api/home.js";
import { sessionTake, getToken, removeToken } from "../../util/validate.js";
import { getCode } from "../../util/address.js";
import { mapActions, mapMutations } from "vuex";
import achievement from "../../components/achievement.vue";
export default {
  name: "App",
  components: {
    information,
    bannerSwipt,
    serviceProvider,
    searchBar,
    government,
    achievement
  },
  head() {
    return {
      title: this.title,
      meta: [
        {
          charset: "utf-8"
        },
        {
          name: "baidu-site-verification",
          content: this.metaCode
        },
        {
          hid: "keywords",
          name: "keywords",
          content: this.keywords
        },
        {
          hid: "description",
          name: "description",
          content: this.description
        }
      ]
    };
  },
  data() {
    return {
      imgBaseUrl: imgBaseUrl,
      festivalVisible: true,
      noticeData: [],
      scramData: [],
      newsData: [],
      achieveData: [], //科技成果传值
      demandData: [], //科技成果传值
      backgroundImage: "",
      metaCode: "code-y3syWFTElo",
      title:
        "比目镜-科技产业政策推送查询平台-企业项目申报立项查询网-高新技术企业认定网",
      keywords:
        "比目镜，政策查询，政策智能推送，最新政策，政策解读，政策匹配，政府黄页，企业名单，企业查询，企业项目立项查询，高新技术企业名单查询，高企名单，专利查询，商标查询，专利交易信息发布，高新技术企业认定，科技项目申报",
      description:
        "比目镜(51bmj.cn)，科技产业政策项目数据平台，提供行业扶持资助补贴政策查询、政府部门最新政策实时智能推送、高新技术企业公示名单查询、高新技术企业项目立项查询、高新技术企业荣誉资质查询、高新技术企业商标专利查询、高新技术企业名单统计、科技项目申报查询、政策项目智能匹配、园区孵化器企业查询、政策扶持项目申报服务"
    };
  },
  async asyncData({ store, route, req }) {
    let title, description, keywords, url, metaCode, backgroundImage;
    let metaInfo = {};
    let province = sessionTake("provinceName");
    let city = sessionTake("cityName");
    let area = "";
    if (req) {
      url = req.rawHeaders[1];
      metaInfo = await getPageMetaInfo({ domain: url });
      let code = metaInfo.result.siteVerification;
      if (code !== undefined && code !== null) {
        metaCode = metaInfo.result.siteVerification;
      } else {
        metaCode = "code-y3syWFTElo";
      }
      title = metaInfo.result.title;
      description = metaInfo.result.description;
      keywords = metaInfo.result.keywords;
      province = metaInfo.result.province;
      city = metaInfo.result.city;
    }
    let params1 = {
      beginDate: "",
      dq: getCode(province, city, area),
      endDate: "",
      pageIndex: 0,
      pageSize: 10,
      policyId: "",
      source: "",
      title: "",
      userID: ""
    };
    let params2 = {
      province: province,
      city: city
    };
    if (city == "全国") {
      params2.province = "";
      params2.city = "";
    }
    let noticeData = {};
    let scramData = {};
    let newsData = {};
    if (sessionTake("provinceName") === undefined) {
      store.commit("setHome", false);
    }
    // 科技成果
    let achieve = {};
    let demand = {};
    // 政策列表
    noticeData = await queryPolicyList(params1, url);
    scramData = await interpretList(params2, url);
    newsData = await newsList(params2, url);
    achieve = await achievementsList({ pageSize: 5, pageNo: 1 });
    demand = await demandList({ pageSize: 5, pageNo: 1 });
    // 首页背景图
    let { result } = await getHomeBackgroundConfigInfo();
    backgroundImage = result[0].imageUrl;

    return {
      title: title,
      description: description,
      keywords: keywords,
      metaCode: metaCode,
      noticeData: noticeData.data.content,
      scramData: scramData.result.records,
      newsData: newsData.result.records,
      achieveData: achieve.result.records,
      demandData: demand.result.records,
      backgroundImage: backgroundImage
    };
  },
  watch: {
    "$store.state.getHome"(newval, olval) {
      this.getPolicyInfo();
      this.$refs.gover.getCityName();
    }
  },
  mounted() {
    this.init();
  },
  methods: {
    ...mapActions(["getUserInfo", "handleLogout"]),
    ...mapMutations(["setToken", "setHome"]),
    async getPolicyInfo() {
      this.setHome(false);
      let province = sessionTake("provinceName");
      let city = sessionTake("cityName");
      let area = "";
      let params1 = {
        beginDate: "",
        dq: getCode(province, city, area),
        endDate: "",
        pageIndex: 0,
        pageSize: 10,
        policyId: "",
        source: "",
        title: "",
        userID: ""
      };
      let params2 = {
        province: province,
        city: city
      };
      if (city == "全国") {
        params2.province = "";
        params2.city = "";
      }
      let noticeData = await queryPolicyList(params1);
      let scramData = await interpretList(params2);
      let newsData = await newsList(params2);
      this.noticeData = noticeData.data.content;
      this.scramData = scramData.result.records;
      this.newsData = newsData.result.records;
    },
    init() {
      let nowTime = new Date().getTime();
      let endTime = localStorage.getItem("endTime");
      console.log(endTime, nowTime);
      console.log(endTime && endTime < nowTime);
      if ((endTime !== null && endTime < nowTime) || endTime == null) {
        this.handleLogout();
        removeToken();
      } else {
        let token = getToken();
        this.setToken(token);
        this.getUserInfo();
      }
    },
    handleClose() {
      this.festivalVisible = false;
    },
    toVip() {
      this.festivalVisible = false;
      window.open("/pageVIP", "_blank");
    }
  }
};
</script>
<style lang="less" scoped>
.addcolor {
  background-color: #f4f4f4;

  .bg_img {
    width: 100%;
    height: 540px;
    background-color: rgba(0, 0, 0, 0.2);
  }
}

.festival {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;

  .photo {
    width: 400px;
    // height: 250px;

    img {
      width: 400px;
      // height: 250px;
      margin-left: 20px;
    }
  }

  .see-btn {
    margin-top: 20px;
    padding: 10px 50px;
    border-radius: 50px;
    color: #ffffff;
    font-size: 18px;
    background: linear-gradient(90deg, #1d6d40, #30855f);
  }
}
</style>
